<?php
/**
 * 产品管理 按道理应该有类型表 做基表
 * 产品类型管理
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;

class Productmgr extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Productmgr');
    }

    /**
     * 产品列表
     * 
     */
    public function productList(){
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');
        $where=array();
        $page = max(1,intval($page));
        $pagesize = 10;
        
       $where['is_delete'] = 0; //回头问下
      
        if(!empty($keyword)){
            $where['title'] = array('like','%'.$keyword.'%');
        }

        $list = $this->model->listWherePage($where,'id desc',($page-1)*$pagesize,$pagesize);

        $total = $this->model->countWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total,'countpage'=>ceil($total/$pagesize)]]);
   
    
    
    }

    /**
     * 企业基本信息
     * @return \think\response\Json|void
     */
    public function productDetail(){
       
        $id = $this->request->param('id');
        $where=array('id'=>$id,'is_delete'=>0);
        // $where['id'] = $id;
        // $where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     * 新增产品
     * @return \think\response\Json|void
     */
    public function addProduct(){ 
        $title = $this->request->param('title');
        // $company_name_en = $this->request->param('company_name_en');        
        // $create_uid  = $this->admin['id'];  

        $data = array('title'=>$title,'status'=>1);
       
        
        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    public function editProduct(){
        $id = $this->request->param('id');       
        // $create_uid  = $this->admin['id'];      
        $title = $this->request->param('title');   
        $status = $this->request->param('status');
        $data = array('title'=>$title);
       
        if($status){$status=1;} else{ $status=0;}  //开启与隐藏的操作
        $data['status']=$status;
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误，请核查']);
        }

        $where['id'] = $id;
        $r = $this->model->updateBywhere($data,$where);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 删除
     */
    public function delProduct(){
        $id = $this->request->param('id');
        $create_uid  = $this->admin['id'];

        // $where = ['id'=>$id,'create_uid'=>$create_uid,'is_delete'=>0];
        $where = ['id'=>$id,'is_delete'=>0];
        $r = $this->model->countWhere($where);

        if(empty($r)){
            return json(['code'=>0,'msg'=>'记录不存在，请核实']);
        }

        $r = $this->model->deleteWhere(['id'=>$id]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }


    /**
     * 产品管理批量 删除
     */
    public function delMoreProduct(){
        $id = $this->request->param('id');
        $create_uid  = $this->admin['id'];           
        $map['id'] = array( 'in',explode(',',$id));    
        $r = $this->model->deleteWhere($map); 
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }




}